import Vue from 'vue';
import VueI18n from 'vue-i18n';
import Cookies from 'js-cookie';
import zhHans from 'vuetify/es5/locale/zh-Hans';
import en from 'vuetify/es5/locale/en';
import th from 'vuetify/es5/locale/th';
import enLocal from './en'
import zhLocal from './zh'
import thLocal from './th'

Vue.use(VueI18n);

const messages = {
  en: {
    ...enLocal,
    ...en
  },
  zh: {
    ...zhLocal,
    ...zhHans
  },
  th: {
    ...thLocal,
    ...th
  }
}

export function getLanguage() {
  const chooseLanguage = Cookies.get('language')
  if (chooseLanguage) return chooseLanguage

  // if has not choose language
  const language = (navigator.language || navigator.browserLanguage).toLowerCase()
  const locales = Object.keys(messages)
  for (const locale of locales) {
    if (language.indexOf(locale) > -1) {
      return locale
    }
  }
  return 'zh'
}

const i18n = new VueI18n({
  // set locale
  // options: en | zh | th
  locale: getLanguage(),
  // set locale messages
  messages,
  silentTranslationWarn: true
})

export default i18n
